package arrayList;

public class MyArrayList {

    private String[] data = null;

    private int size = 0;

    public MyArrayList() {
        data = new String[10];
    }

    public MyArrayList(int size) {
        if (size < 10) {
            size = 10;
        }
        data = new String[size];
    }

    //扩容
    public void resize() {
        String[] newData = new String[size + (size >> 1)];
        for (int i = 0;i < size;i++) {
            newData[i] = data[i];
        }
        data = newData;
    }

    //尾插
    public void add(String elem) {
        if (size >= data.length) {
            resize();
        }
        data[size] = elem;
        size++;
    }

    ///**
    //     * 判断当前的顺序表是不是满的！
    //     * @return true:满   false代表空
    //     */
    public boolean isFull() {
        if (size == data.length) {
            return true;
        }
        return false;
    }

    //按照下标添加
    public void add(int index,String elem) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("index:" + index + ",size" + size);
        }
        if (size >= data.length) {
            resize();
        }
        for (int i = size - 1;i >= index;i--) {
            data[i + 1] = data[i];
        }
        data[index] = elem;
        size++;
    }

    //按照下标删除
    public String remove(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index = " + index + ",size = " + size);
        }
        String elem = data[index];
        for (int i = index;i < size - 1;i++) {
            data[i] = data[i + 1];
        }
        size--;
        return elem;
    }

    //按照值删除
    public boolean remove(String elem) {
        int n = 0;
        for (;n < size;n++) {
            if (data[n] == elem) {
                break;
            }
            if (n == size) {
                return false;
            }
            for (int i = n;i < size - 1;i++) {
                data[i] = data[i + 1];
            }
        }
        size--;
        return true;
    }

    //按照下标输出
    public String get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index = " + index + ",size = " + size);
        }
        return data[index];
    }

    //按照下标修改
    public void set(int index,String elem) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index = " + index + ",size = " + size);
        }
        data[index] = elem;
    }

    //清空顺序表
    public void ciear() {
        size = 0;
    }

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0;i < size;i++) {
            stringBuilder.append(data[i]);
            if (i < size - 1) {
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        MyArrayList list = new MyArrayList();
        list.add("hello");
        list.add("world");
        list.add("world");
        list.add("world");
        list.add("world");
        list.add("world");
        list.add("world");
        list.add("world");
        list.add("world");
        list.add("world");
        list.add("world");
        list.add("world");
        System.out.println(list);
    }
}
